package org.example.codetrail.utils;

import org.example.codetrail.pojo.User;
import org.example.codetrail.pojo.UserUseInRepo;

import java.util.HashMap;
import java.util.Map;

public class UserChange {

    public static String mapChangeString(Map<String,String> map){
        if (map == null)return null;
        StringBuilder sb = new StringBuilder();
        map.forEach((key, value) -> sb.append(key).append(":").append(value).append(";"));
        return sb.toString();
    }
    public static Map<String,String> stringChangeMap(String str){
        Map<String, String> map = new HashMap<>();
        if (str == null)return null;
        String[] pairs = str.split(";");
        for (String pair : pairs) {
            if (!pair.isEmpty()) {
                String[] keyValue = pair.split(":");
                if (keyValue.length == 2) {
                    map.put(keyValue[0], keyValue[1]);
                }
            }
        }
        return map;
    }
    public static User getUser(UserUseInRepo uuir){
        User user = new User();
        if (uuir==null)return null;
        user.setId(uuir.getId());
        user.setUsername(uuir.getUsername());
        user.setPassword(uuir.getPassword());
        user.setAvatar(uuir.getAvatar());
        user.setEmail(uuir.getEmail());
        user.setPhone(uuir.getPhone());
        user.setGender(uuir.getGender());
        user.setAge(uuir.getAge());
        user.setTags(uuir.getTags());
        user.setBiography(uuir.getBiography());
        user.setAccessToken(stringChangeMap(uuir.getAccessToken()));
        return user;
    }
    public static UserUseInRepo getUserUseInRepo(User user){
        UserUseInRepo uuir = new UserUseInRepo();
        uuir.setId(user.getId());
        uuir.setUsername(user.getUsername());
        uuir.setPassword(user.getPassword());
        uuir.setAvatar(user.getAvatar());
        uuir.setEmail(user.getEmail());
        uuir.setPhone(user.getPhone());
        uuir.setGender(user.getGender());
        uuir.setAge(user.getAge());
        uuir.setTags(user.getTags());
        uuir.setBiography(user.getBiography());
        uuir.setAccessToken(mapChangeString(user.getAccessToken()));
        return uuir;
    }
}
